package com.wangbl.infomanager;

import java.util.HashSet;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;

import com.wangbl.infomanager.utils.Logger;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class LoginActivity extends Activity {

	protected static final String TAG = "LoginActivity";
	private WebView wv;
//	private ProgressBar pb;
	private Dialog dialog;
	private String url = "http://irmsma.ruc.edu.cn/rucjson/dl.php";
	private SharedPreferences sp;
	private Editor edit;

	@SuppressLint("SetJavaScriptEnabled")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		dialog = new Dialog(this, R.style.CustomProgressDialog);
		dialog.setContentView(R.layout.progress_dialog);
		dialog.show();
		dialog.setCancelable(false);
		sp = getSharedPreferences("config", Context.MODE_PRIVATE);
		edit = sp.edit();
		wv = (WebView) findViewById(R.id.login_webview);
		WebSettings setting = wv.getSettings();
		setting.setCacheMode(WebSettings.LOAD_DEFAULT); //LOAD_DEFAULT
		setting.setJavaScriptEnabled(true);
		wv.setWebViewClient(new WebViewClient() {
			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				super.onPageStarted(view, url, favicon);
				try {
					dialog.show();
				} catch (Exception e) {
				}
			}
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				Logger.i(TAG, "shouldOverrideUrlLoading:" + url);
				// 04-18 12:50:46.962: I/LoginActivity(26821):
				// shouldOverrideUrlLoading:http:
				// irmsma.ruc.edu.cn/rucjson/tz.php?username=20119976
				parser(url);
				return false;
			}

			@Override
			public void onReceivedSslError(WebView view,
					SslErrorHandler handler, SslError error) {
				toast(error.toString());
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
				Logger.i(TAG, "onPageFinished");
				try {
					dialog.dismiss();
				} catch (Exception e) {
				}
				
			}

			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				super.onReceivedError(view, errorCode, description, failingUrl);
				try {
					dialog.dismiss();
				} catch (Exception e) {
				}
				toast(description);
			}

			@Override
			public void onReceivedLoginRequest(WebView view, String realm,
					String account, String args) {
				Logger.i(TAG, realm + "   " + account + "  " + args);

			}

		});
		wv.loadUrl(url);
	}
	
	private void parser(String url){
		Logger.i(TAG, "parser:"+url);
		if (url != null) {
			
//			int index2 = url.indexOf("ticket=");
//			if (index2 != -1) {
//				String str = url.substring(index2);
//				String[] strs = str.split("=");
//				if (strs != null && strs.length > 1) {
//					edit.putString("ticket", strs[1]);
//					edit.apply();
//				} 
//			}
			
			int index = url.indexOf("username=");
			if (index != -1) {
				String str = url.substring(index);
				String[] strs = str.split("=");
				if (strs != null && strs.length > 1) {
					edit.putString("username", strs[1]);
					edit.apply();
					toast("登录成功...");
					JPushInterface.resumePush(this);
					Set<String> tags = new HashSet<String>();
					tags.add(strs[1]); 
					JPushInterface.setTags(this, tags, null);
					Intent intent = new Intent(LoginActivity.this, InfoMainActivity.class);
					LoginActivity.this.startActivity(intent);
					finish();
				} else {
					toast("登录失败,请重新登录...");
				}
			}
		}
	}
	
	@Override
	protected void onDestroy() {
		Logger.i(TAG, "onDestroy");
		super.onDestroy();
		wv.destroy();
	}

	private void toast(String content) {
		Toast.makeText(this.getApplicationContext(), content,
				Toast.LENGTH_SHORT).show();
	}

}
